﻿/**
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.thorqq.magictimer.action;

//import com.android.deskclock2.R;

import com.thorqq.magictimer.Configuration;
import com.thorqq.magictimer.misc.AlarmAlertWakeLock;
import com.thorqq.magictimer.timeLoop.Timer;
import com.thorqq.magictimer.timeLoop.TimerMgr;
import com.thorqq.magictimer.util.Util;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Vibrator;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

/***
 * Manages alarms and vibe. Runs as a service so that it can continue to play if
 * another activity overrides the AlarmAlert dialog.
 */
// 震动
public class ActionVibratorSvc extends Service
{
    private static final long[] sVibratePattern = new long[] { 500, 500 };

    private boolean mPlaying = false;
    private Vibrator mVibrator;
    private Timer mCurrentAlarm;
    private long mStartTime;
    private TelephonyManager mTelephonyManager;
    private int mInitialCallState;

    // Internal messages
    private static final int KILLER = 1000;
    private Handler mHandler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
            case KILLER:
                sendKillBroadcast((Timer) msg.obj);
                stopSelf();
                break;
            }
        }
    };

    private PhoneStateListener mPhoneStateListener = new PhoneStateListener()
    {
        @Override
        public void onCallStateChanged(int state, String ignored)
        {
            // The user might already be in a call when the alarm fires. When
            // we register onCallStateChanged, we get the initial in-call state
            // which kills the alarm. Check against the initial call state so
            // we don't kill the alarm during a call.
            // 如果有通话进来，则停止闹铃
            if (state != TelephonyManager.CALL_STATE_IDLE && state != mInitialCallState)
            {
                sendKillBroadcast(mCurrentAlarm);
                stopSelf();
            }
        }
    };

    @Override
    public void onCreate()
    {
        mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        // Listen for incoming calls to kill the alarm.
        // 监听通话状态
        mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        // 管理屏幕亮度和键盘锁
        AlarmAlertWakeLock.acquireCpuWakeLock(this);
    }

    @Override
    public void onDestroy()
    {
        // 停止闹铃
        stop();
        // Stop listening for incoming calls.
        // 停止监听来电状态
        // mTelephonyManager.listen(mPhoneStateListener, 0);
        AlarmAlertWakeLock.releaseCpuLock();
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    // 客户端调用 startService(Intent)启动服务的时候会调用此方法
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        // No intent, tell the system not to restart us.
        if (intent == null)
        {
            stopSelf();
            return START_NOT_STICKY;
        }

        ActionMgr.getIntance().registerSvc(intent.getAction());
        final Timer alarm = intent.getParcelableExtra(TimerMgr.ALARM_INTENT_EXTRA);

        if (alarm == null)
        {
            stopSelf();
            return START_NOT_STICKY;
        }

        // 已经存在alarm，则先删除当前的
        if (mCurrentAlarm != null)
        {
            sendKillBroadcast(mCurrentAlarm);
        }

        // 开始播放和震动
        play(alarm);
        mCurrentAlarm = alarm;
        // Record the initial call state here so that the new alarm has the
        // newest state.
        // 记录当前的呼叫状态
        mInitialCallState = mTelephonyManager.getCallState();

        return START_STICKY;
    }

    // 通知界面此闹铃被删掉了
    private void sendKillBroadcast(Timer alarm)
    {
        long millis = System.currentTimeMillis() - mStartTime;
        int minutes = (int) Math.round(millis / 60000.0);
        Intent alarmKilled = new Intent(TimerMgr.ALARM_KILLED);
        alarmKilled.putExtra(TimerMgr.ALARM_INTENT_EXTRA, alarm);
        alarmKilled.putExtra(TimerMgr.ALARM_KILLED_TIMEOUT, minutes);
        sendBroadcast(alarmKilled);
    }

    private void play(Timer alarm)
    {
        // stop() checks to see if we are already playing.
        // 先停止铃声和震动
        stop();

        // 震动
        mVibrator.vibrate(sVibratePattern, 0);

        // 在指定时间后删除闹铃声音
        enableKiller(alarm);
        mPlaying = true;
        mStartTime = System.currentTimeMillis();
    }

    /***
     * Stops alarm audio and disables alarm if it not snoozed and not repeating
     */
    // 停止铃声和震动，禁用闹铃
    public void stop()
    {
        if (mPlaying)
        {
            mPlaying = false;

            Intent alarmDone = new Intent(TimerMgr.ALARM_DONE_ACTION);
            sendBroadcast(alarmDone);

            // 停止震动
            mVibrator.cancel();
        }
        // 删除消息接收
        disableKiller();
    }

    /***
     * Kills alarm audio after ALARM_TIMEOUT_SECONDS, so the alarm won't run all
     * day.
     * 
     * This just cancels the audio, but leaves the notification popped, so the
     * user will know that the alarm tripped.
     */
    // 在指定时间后删除闹铃声音，这样闹铃就不会整天响了。只关闭声音，通知栏上的通知不管
    private void enableKiller(Timer alarm)
    {
//        final String alarmTimeoutString = PreferenceManager.getDefaultSharedPreferences(this)
//                .getString(ConfigurationActivity.KEY_ALARM_TIMEOUT_SECONDS,
//                        ConfigurationActivity.ALARM_TIMEOUT_SECONDS);
        int alarmTimeoutSeconds = Configuration.getIntance().getAlarmTimeout();

        Util.log("Wait for " + alarmTimeoutSeconds + " seconds");
        mHandler.sendMessageDelayed(mHandler.obtainMessage(KILLER, alarm),
                1000 * alarmTimeoutSeconds);
    }

    private void disableKiller()
    {
        mHandler.removeMessages(KILLER);
    }

}
